<?php
namespace Admin\Controller;
use Think\Controller;
class CategoryController extends BaseController {
    public function index(){
        $nav=D('Category')->getTreeData('tree','order_number,id');
		$this->assign('nav',$nav);
		$this->display();
    }

    /**
	 * 添加
	 */
	public function add(){
		if(IS_POST){
            $data=I('post.');

            $thumb = $_FILES['thumb'];
            if ($thumb['name'] != '') {  //有提交图片
                $thumbpath = $this->uploadoneimg($thumb);
                $thumbpath = $thumbpath['savepath'].$thumbpath['savename'];
                $data['thumb'] = $thumbpath;
            }
            $result=D('Category')->addData($data);
            if($result){
                // 操作成功
                $this->success('添加成功',U('Admin/Category/index'));
            }else{
            	$this->error('添加失败');
            }
        }else{
        	$id=I('id');

        	$nav=D('Category')->getTreeData('tree','order_number,id');
			$this->assign('nav',$nav);
			$this->assign('id',$id);
            $this->display();
        }
	}

	/**
	 * 修改
	 */
	public function edit(){
		if(IS_POST){
			$data=I('post.');
			$map=array(
				'id'=>$data['id']
				);
			$thumb = $_FILES['thumb'];
            if ($thumb['name'] != '') {  //有提交新闻图片
                $thumbpath = $this->uploadoneimg($thumb);
                $thumbpath = $thumbpath['savepath'].$thumbpath['savename'];
                $data['thumb'] = $thumbpath;
            }
			D('Category')->editData($map,$data);
			$this->success('修改成功',U('Admin/Category/index'));
		}else{
			$id=I('id');
	        $data=M('Category')->find($id);
	        $nav=D('Category')->getTreeData('tree','order_number,id');
	        $this->assign('nav',$nav);
	        $this->assign('data',$data);
	        $this->assign('thumb',$data['thumb']);
			$this->display();
		}
	}

	/**
     * 删除缩略图
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delthumb() {
        $id = intval(I('get.id'));
        if ($id) {
            $map['id'] = $id;
            $model=M('Category');
            $thumb = $model->where($map)->getField('thumb');
            $path = $_SERVER['DOCUMENT_ROOT'].__ROOT__.'/upload/';
            delallimg($path,$thumb); //删除图片，包括文件路径
            if ($model->where($map)->setField('thumb','')) {
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
    }

	/**
	 * 删除
	 */
	public function delete(){
		$id=I('get.id');
		$map=array(
			'id'=>$id
			);

        //处理缩略图
        $thumb = M('Category')->where("id= %d",$id)->getField('thumb');   
        $path = $_SERVER['DOCUMENT_ROOT'].__ROOT__.'/upload/';
        delallimg($path,$thumb);//删除图片，包括文件路径

		$result=D('Category')->deleteData($map);
		if($result){
			$this->success('删除成功',U('Admin/Category/index'));
		}else{
			$this->error('请先删除子分类');
		}
	}

	/**
	 * 排序
	 */
	public function order(){
		$data=I('post.');
		D('Category')->orderData($data);
		$this->success('排序成功',U('Admin/Category/index'));
	}
  
}